package com.example.project.controller;


import com.example.project.entity.RequestInto;
import com.example.project.entity.ResponseInfo;
import com.example.project.entity.User;
import com.example.project.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("login")
    public ResponseInfo login(@RequestBody User user) {
        if (!StringUtils.hasLength(user.getAccount())) {
            return ResponseInfo.fail(202, "账号不能为空");
        }

        if (!StringUtils.hasLength(user.getPassword())) {
            return ResponseInfo.fail(202, "密码不能为空");
        }

        ResponseInfo res = userService.login(user);


        return res;

    }

    @RequestMapping("getusertable")
    public ResponseInfo getusertable(User user, RequestInto params) {
        ResponseInfo res = userService.getusertable(user, params);
        return res;
    }

    @RequestMapping("adduser")
    public ResponseInfo adduser(User user) {
        if (!StringUtils.hasLength(user.getName())) {
            return ResponseInfo.fail(202, "名称不能为空");
        }
        if (!StringUtils.hasLength(user.getAccount())) {
            return ResponseInfo.fail(203, "账号不能为空");
        }
        if (user.getBirthday() == null) {
            return ResponseInfo.fail(204, "出生日期不能为空");
        }
        ResponseInfo res = userService.adduser(user);
        return res;
    }

    @RequestMapping("edituser")
    public ResponseInfo edituser(User user) {
        if (!StringUtils.hasLength(user.getName())) {
            return ResponseInfo.fail(202, "名称不能为空");
        }
        if (!StringUtils.hasLength(user.getAccount())) {
            return ResponseInfo.fail(203, "账号不能为空");
        }
        if (user.getBirthday() == null) {
            return ResponseInfo.fail(204, "出生日期不能为空");
        }
        ResponseInfo res = userService.edituser(user);
        return res;
    }

    @RequestMapping("deluser")
    public ResponseInfo deluser(User user) {
        ResponseInfo res = userService.deluser(user);
        return res;
    }

    //6.1的查找管理员
    @RequestMapping("/getadmin")
    public ResponseInfo getadmin() {
        ResponseInfo res = userService.getadmin();
        System.out.println(res);
        return res;
    }

}
